<template>
    <h1>病房添加</h1>
    <table class="table" border="1">
        <tbody>
            <tr>
                <td>病房名称</td>
                <td>
                    <input type="text" v-model="Info.bName"/>
                </td>
            </tr>
            <tr>
                <td>所属科室</td>
                <td >
                    <select v-model="Info.kId">
                        <option value="-1">--请选择--</option>
                        <option v-for="item in bang" :value="item.kId">{{ item.kName }}</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>床位数</td>
                <td>
                    <input type="text" v-model="Info.bedSum"/>
                </td>
            </tr>
            <tr>
                <td>状态</td>
                <td>
                    <input type="radio" :value="true" v-model="Info.state"/>启用
                    <input type="radio" :value="false" v-model="Info.state"/>禁用
                </td>
            </tr>
            <tr>
                <td>空床数</td>
                <td>
                    <input type="text" v-model="Info.koBedSum"/>
                </td>
            </tr>
            <tr>
                <td>病房创建时间</td>
                <td>
                    <input type="date" v-model="Info.craDate"/>
                </td>
            </tr>
            <tr>
                <td>病房更新时间</td>
                <td>
                    <input type="date" v-model="Info.gengDate"/>
                </td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center;">
                    <input type="button" value="保存" @click="Add"/>
                </td>
            </tr>
        </tbody>
    </table>
</template>

<script setup lang="ts">
import { ref,onMounted } from "vue";
import axios from "axios";
import { useRouter } from "vue-router";
onMounted(()=>{
    bing();
})

const bang = ref([{
    "kId": 0,
  "kName": "",
}])

const bing=()=>{
    axios.get("https://localhost:7199/api/BingRoom/GetKeAll")
    .then(res=>{
        bang.value=res.data
        console.log(bang.value)
    })
    .catch(error=>{
        console.log(error);
    })
}

const Info=ref({
    "bId": 0,
  "bName": "",
  "kId": 0,
  "bedSum": "",
  "state": true,
  "koBedSum": "",
  "craDate": "",
  "gengDate": ""
})

const router=useRouter();
const Add=()=>{
    if(Info.value.bName.length>6){
        alert("病房名称不能超过6个字符")
    }
    if(Info.value.bName==""){
        alert("病房名称不能为空")
    }
    axios.post("https://localhost:7199/api/BingRoom/Add",Info.value)
    .then(res=>{
        if(res.data==-1){
            alert("用户名已存在！");
        }
        else if(res.data>0){
            alert("添加成功！")
            router.push("/ShowBingRoom")
        }
        else{
            alert("添加失败")
        }
    })
    .catch(error=>{
        console.log(error);
    })
}
</script>